package com.das.mechanic_base.adapter.groundpush;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.kproduce.roundcorners.RoundTextView;

import java.util.ArrayList;
import java.util.List;

public class X3FirstCarAdapter extends RecyclerView.Adapter<X3FirstCarAdapter.X3FirstCarHolder> {

    private List<String> mList;

    private List<String> sList;

    private Context mContext;

    public X3FirstCarAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeSelectCar(List<String> sList,List<String> mList) {
        this.sList = sList;
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public X3FirstCarHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new X3FirstCarHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_first_car_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull X3FirstCarHolder holder, int position) {

        holder.rv_title.setText(mList.get(position));

        String answer = mList.get(position);
        if (sList.contains(answer)) {
            holder.rv_title.setBackgroundColor(Color.parseColor("#10f64444"));
            holder.rv_title.setStrokeColor(Color.parseColor("#f64444"));
            holder.rv_title.setTextColor(Color.parseColor("#f64444"));

        }else {
            holder.rv_title.setBackgroundColor(Color.WHITE);
            holder.rv_title.setStrokeColor(Color.parseColor("#f0f0f5"));
            holder.rv_title.setTextColor(Color.parseColor("#333333"));
        }

    }

    IOnSelectFirstCar iOnSelectFirstCar;

    public X3FirstCarAdapter setiOnSelectFirstCar(IOnSelectFirstCar iOnSelectFirstCar) {
        this.iOnSelectFirstCar = iOnSelectFirstCar;
        return this;
    }

    public interface IOnSelectFirstCar {
        void iOnSelectCar(List<String> sList);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class X3FirstCarHolder extends RecyclerView.ViewHolder {
        RoundTextView rv_title;
        public X3FirstCarHolder(@NonNull View itemView) {
            super(itemView);
            rv_title = itemView.findViewById(R.id.rv_title);

            rv_title.setOnClickListener(v -> {
                String content = mList.get(getAbsoluteAdapterPosition());
                if (sList.contains(content)) {
                    sList.remove(content);
                }else {
                    sList.add(content);
                }
                notifyItemChanged(getAbsoluteAdapterPosition());
                if (iOnSelectFirstCar != null) {
                    iOnSelectFirstCar.iOnSelectCar(sList);
                }
            });
        }
    }
}
